from datetime import datetime

def process_file(filename):
    with open(filename, "r", encoding="utf-8") as file:
        lines = file.readlines()

    dates = set()
    quotes = []
    processed_lines = []

    for line in lines:
        stripped_line = line.strip()
        if stripped_line:
            if stripped_line.startswith("## "):
                dates.add(stripped_line)
                quotes.append("")
            else:
                quotes[-1] = stripped_line

    sorted_dates = sorted(dates, key=lambda x: datetime.strptime(x[3:].strip(), "%Y-%m-%d"))

    for date in sorted_dates:
        processed_lines.append(f"{date}\n{quotes.pop(0)}\n")

    with open(filename, "w", encoding="utf-8") as file:
        file.writelines(processed_lines)

# 主程序
if __name__ == "__main__":
    process_file("quotes1.txt")